<?php
namespace app\admin\controller;
use app\admin\service\ShopService;
use think\Controller;
use \think\Db;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/26
 * Time: 15:38
 */
/**
 * Class Count 统计
 * @package app\admin\controller
 */
class Count extends Controller
{
    //店铺列表
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        //条件查询
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['b.paytime'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['b.paytime'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['b.paytime'] = ['<= time', $endTime];
            }
        }

        $rs=db::name('shop')->alias('a')
            ->join('mj_order b','a.shopId=b.shopId','left')
            ->field('a.shopId,a.shopName,sum(reward) as dspay,
                sum(case when paymentWay=2 then discountMoney else 0 end) as xjpay,
                sum(case when paymentWay=1 then discountMoney else 0 end)  as wxpay,
                sum(shopIncome) as sppay,
                sum(systemIncome) as ptpay,
                sum(discountMoney) as discountMoney,
                sum(subsidy) as btpay,
                sum((subsidy+discountMoney)*0.8) as jspay')
            ->where('b.payStatus',2)
            ->where('b.status',3)
            ->where($where)
            //->fetchsql()
            ->group('a.shopId')
            ->select();
            if(!empty($rs)){
                foreach ($rs as $k => $v) {
                    $rs[$k]['tkpay']=db::name('order')->where('shopId',$v['shopId'])->where('status',5)->sum('discountMoney');
                    
                }
            }   
        //按钮收入
        $pt=db::name('order')->where('payStatus',2)->where('status',3)->sum('systemIncome');
        $vipPay=db::name('vip_order')->where('status','>',1)->sum('amount');
        $appointment=db::name('appointment')->whereTime('payTime', 'yesterday')->where('payStatus',2)->sum('money');
        //昨日收入
        $time1=db::name('order')->whereTime('paytime', 'yesterday')->where('paymentWay',1)->where('payStatus',2)->sum('discountMoney');
        $time2=db::name('vip_order')->whereTime('updateTime', 'yesterday')->where('status',2)->sum('amount');
        $time3=db::name('appointment')->whereTime('payTime', 'yesterday')->where('payStatus',2)->sum('money');
        $this->assign('time',($time1+$time2+$time3)*0.994);
        $this->assign('vipPay',$vipPay+$appointment);
        $this->assign('ptCount',$pt);
        $this->assign('rs',$rs);
        return $this->fetch();
    }


    //员工统计
    public function ygcount(){
        $param = $this->request->param();
        $where = [];
        //条件查询
        $startTime = empty($param['start_time']) ? 0 : strtotime($param['start_time']);
        $endTime   = empty($param['end_time']) ? 0 : strtotime($param['end_time']);
        if (!empty($startTime) && !empty($endTime)) {
            $where['b.paytime'] = [['>= time', $startTime], ['<= time', $endTime]];
        } else {
            if (!empty($startTime)) {
                $where['b.paytime'] = ['>= time', $startTime];
            }
            if (!empty($endTime)) {
                $where['b.paytime'] = ['<= time', $endTime];
            }
        }


        $rs=db::name('staff')->alias('a')
        ->join('mj_order b','a.userId=b.staffId','left')
        ->field('a.userName,sum(b.discountMoney) as discountMoney,count(b.staffId) as num')
        ->group('b.staffId')
        ->where($where)
        ->where('payStatus',2)
        ->paginate(10);

        $this->assign('arr',$rs);
        return $this->fetch();
    }
    


}
